package org.jfinanceobserver.utillities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This is the utile class
 *
 * $Author: r78v10a07@gmail.com $ $LastChangedDate: 2013-02-11 14:03:43 +0100
 * (Mon, 11 Feb 2013) $ $LastChangedRevision: 34 $
 *
 * @since Feb 10, 2013
 */
public class Util {

    private static Util singleton;

    private Util() {
    }

    /**
     * Return a Utile
     *
     * @return a Utile
     */
    public static synchronized Util getInstance() {
        if (singleton == null) {
            singleton = new Util();
        }
        return singleton;
    }
    /**
     * The market open hour
     */
    public String openHour = "14:00:00";
    /**
     * The market close hour
     */
    public String closeHour = "22:01:00";
    /**
     * The time interval to retrieve data from the Yahoo finance URL during the
     * open time
     */
    public long openInterval = 30000;
    /**
     * The time interval to wait during the close time
     */
    public long closeInterval = 3600000;

    /**
     * Get the open date
     *
     * @return the open date
     * @throws ParseException
     */
    public Date openDate(){
        try {
            return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse((new SimpleDateFormat("yyyy/MM/dd")).format(new Date()) + " " + openHour);
        } catch (ParseException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * Get the close date
     *
     * @return the close date
     * @throws ParseException
     */
    public Date closeDate(){
        try {
            return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse((new SimpleDateFormat("yyyy/MM/dd")).format(new Date()) + " " + closeHour);
        } catch (ParseException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * Get the open hour
     * @return the open hour
     */
    public String getOpenHour() {
        return openHour;
    }

    /**
     * Set the open hour
     * @param openHour the open hour
     */
    public void setOpenHour(String openHour) {
        this.openHour = openHour;
    }

    /**
     * Get the close hour
     * @return the close hour
     */
    public String getCloseHour() {
        return closeHour;
    }

    /**
     * Set the close hour
     * @param closeHour the close hour
     */
    public void setCloseHour(String closeHour) {
        this.closeHour = closeHour;
    }

    /**
     * Get the open time interval
     * @return the open time interval
     */
    public long getOpenInterval() {
        return openInterval;
    }

    /**
     * Set the open time interval
     * @param openInterval the open time interval
     */
    public void setOpenInterval(long openInterval) {
        this.openInterval = openInterval;
    }

    /**
     * Get the close time interval
     * @return the close time interval
     */
    public long getCloseInterval() {
        return closeInterval;
    }

    /**
     * Set the close time interval
     * @param closeInterval the close time interval
     */
    public void setCloseInterval(long closeInterval) {
        this.closeInterval = closeInterval;
    }
}
